<template>
  <div>
    <router-link tag="div" class="header-abs" to="/" v-show="showAbs">
      <div class="iconfont header-abs-back">&#xe624;</div>
    </router-link>
    <div class="header-fixed" v-show="!showAbs" :style="styleObj">
      {{sightName}}
      <router-link to="/">
        <div class="iconfont header-fixed-back">&#xe624;</div>
      </router-link>
    </div>
  </div>
</template>
<script>
export default {
  name: 'DetailHeader',
  props: {
    sightName: String
  },
  data () {
    return {
      showAbs: true,
      styleObj: {
        opacity: 0
      }
    }
  },
  methods: {
    handleScroll () {
      const top = this.getScrollTop()
      if (top > 10) {
        this.showAbs = false
        this.styleObj.opacity = (top / 180) > 1 ? 1 : (top / 180)
      } else {
        this.showAbs = true
      }
    },
    getScrollTop () {
      let scrollTop = 0
      if (document.documentElement && document.documentElement.scrollTop) {
        scrollTop = document.documentElement.scrollTop
      } else if (document.body) {
        scrollTop = document.body.scrollTop
      }
      return scrollTop
    }
  },
  activated () {
    window.addEventListener('scroll', this.handleScroll)
  },
  deactivated () {
    window.removeEventListener('scroll', this.handleScroll)
  }
}
</script>
<style lang="stylus" scoped>
@import "~styles/varibles.styl"
.header-abs
  position absolute
  top .1rem
  left .1rem
  height .72rem
  width .72rem
  border-radius .36rem
  background rgba(0, 0, 0, .5)
  text-align center
  line-height .72rem
  .header-abs-back
    color #fff
    font-size .32rem
.header-fixed
  z-index 2
  position fixed
  top 0
  left 0
  right 0
  height $headerHeight
  line-height $headerHeight
  background-color $bgColor
  text-align center
  color #fff
  font-size .32rem
  .header-fixed-back
    width .64rem
    font-size .4rem
    position absolute
    top 0
    left 0
    color #fff
</style>
